﻿<script>

    var _sel_districts1 = @this.RawSerialize(this.ViewBag.PartList);
    var _roles = @this.RawSerialize(this.ViewBag.ListRole); 
    var _sts = [{ "id": "0", "text": "信息缺失" }, { "id": "1", "text": "待审核" }, { "id": "2", "text": "已审核" }]; 

    _roles.push({ "ID": 0, "RoleName": "普通用户", "ST": 1, "Percent": 0, "Price": 0 });
    _roles.push({ "ID": 99, "RoleName": "VIP", "ST": 1, "Percent": 0, "Price": 0 });

    var _vm;
    $(function () {
        var vm = new PageViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.init();


    });



    function PageViewModel() {
        var me = this;
        ViewModel.call(me);
        vmExtend.call(me);

        me.url = "/Wiki/Users/GetModels";
        me.deleteUrl = "/Wiki/Users/Delete";
        me.dialog = new FormDialog(me);

        me.revisePasswordDialog = new RevisePasswordDialog();
        me.openRevisePasswordDialog = function () {
            me.revisePasswordDialog.open(me.dataTable.selectedModel());
        }
     }
    function vmExtend() {
        var me = this;
    }

    function FormDialog(vm) {
        var me = this;
        Dialog.call(me);

        

        me._sel_districts2 = _oba([]);
        me._sel_districts3 = _oba([]);

        me.onOpen = function ()
        {
            

            var model = me.editModel();
            if (model)
            {
                var dataModel = model.Data;
                var bindModel = $ko.toJS(model);

                if (bindModel.ProvinceID != "0" && bindModel.ProvinceID)
                { 
                    $ace.get("/Wiki/Users/GetDistrictList", {A:"1", Pid: bindModel.ProvinceID }, function (result) {
                        if (result.Status ==100)
                        {
                            me._sel_districts2(result.Data);
                        }
                    });
                }
                if (bindModel.CityID != "0" && bindModel.CityID)
                {                   

                    $ace.get("/Wiki/Users/GetDistrictList", { A: "2", Pid: bindModel.CityID }, function (result) {
                        if (result.Status == 100)
                        {
                            me._sel_districts3(result.Data);
                        }
                    });
                }
               

                me.model(bindModel);
                setTimeout(function ()
                {
                    $("#Sel_ProvinceID").val(model.ProvinceID());                        
                }, 500);

                setTimeout(function () {
                    console.log(model.CityID());
                    $("#Sel_CityID").val(model.CityID());
                }, 2000);

                setTimeout(function () {
                    console.log(model.PartID());
                    $("#Sel_PartID").val(model.PartID());
                }, 3000);


                $ace.get("/Wiki/Users/GetList", { UserID: bindModel.Id }, function (result) {
                    if (result.Status == 100)
                    {
                        if (result.Data) {
                            var s = "";
                            result.Data.map(function (item) {
                                s += "<tr>";

                                s += "<td>" + item.Title + "</td>";
                                s += "<td>" + item.transaction_date + "</td>";
                                s += "<td>" + item.transaction_price + "</td>";
                                s += "<td>" + item.expire_date + "</td>";
                                s += "</tr>";
                            });
                            jQuery("#paylog").html(s);
                        }
                        
                    }
                    else {

                    }
                });
                

            }
            else {
                me.model({});

            }


            $('#Sel_ProvinceID').change(function () {
                var id = $('#Sel_ProvinceID').val();
                if (id && id!=0) {
                    $ace.get("/Wiki/Users/GetDistrictList", { A: "3", Pid: id }, function (result) {
                        if (result.Status == 100) {
                            me._sel_districts2(result.Data);
                        }
                    });
                }
                
            });
            $('#Sel_CityID').change(function ()
            {
                var id = $('#Sel_CityID').val();
                if (id && id != 0) {
                    $ace.get("/Wiki/Users/GetDistrictList", { A: "4", Pid: id }, function (result) {
                        if (result.Status == 100) {
                            me._sel_districts3(result.Data);
                        }
                    });
                }
                
            });
            
        }

        me.onSave = function ()
        {
            if (!$('#form1').formValid()) {
                return false;
            }

            var model = me.model();




            if (me.editModel())
            {
                model.ProvinceID = $("#Sel_ProvinceID").val() || 0;
                model.CityID = $("#Sel_CityID").val() || 0;
                model.PartID = $("#Sel_PartID").val() || 0;
                model.ST = $("#ST").val() || 0;
                model.RoleID = $("#RoleID").val() || 0;

                $ace.post("/Wiki/Users/Edit", model, function (result)
                {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.dataTable.reload();
                }
                );
            }
            else
            {
                model.ProvinceID = $("#Sel_ProvinceID").val()||0;
                model.CityID = $("#Sel_CityID").val() || 0;
                model.PartID = $("#Sel_PartID").val() || 0;

                model.ST = $("#ST").val() || 0;
                model.RoleID = $("#RoleID").val() || 0;

                $ace.post("/Wiki/Users/Add", model, function (result)
                {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.loadData();
                });
            }
        }
    }


    function RevisePasswordDialog() {
        var me = this;
        Dialog.call(me);
        me.title("重置密码");

        me.onOpen = function () {
            var model = me.editModel();
            if (!model) {
                throw new Error();
            }

            var bindModel = $ko.toJS(model);
            var m = {};
            m.Id = bindModel.Id;
            m.Mobile = bindModel.Mobile;
            m.UserName = bindModel.UserName;

            me.model(m);
        }
        me.onSave = function () {
            if (!$('#RevisePasswordForm').formValid()) {
                return false;
            }

            var model = me.model();
            var pwd = model.UserPass;
            var data = { userId: model.Id, newPassword: pwd };
            $ace.post("/Wiki/Users/RevisePassword", data, function (result) {
                $ace.msg(result.Msg);
                me.close();
            }
            );
        }
    }
</script>
